package com.codechuan.hallo_web;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo_web.common.Constant;
import com.codechuan.hallo_web.thread.MainTrhead;
import com.codechuan.hallo_web.utils.HttpUtil;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@MapperScan(basePackages = "com.codechuan.hallo_web.mapper")
@EnableTransactionManagement
@EnableDiscoveryClient
public class HalloWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(HalloWebApplication.class, args);
        configureTasks();
        Constant.executorService = Executors.newFixedThreadPool(100);
//        Constant.executorService.execute(new MainTrhead());
    }


    public static void configureTasks() {
        String url = "http://api2.xkdaili.com/tools/XApi.ashx?apikey=XKC2651EEA9B252A1436&qty=1&format=json&split=0&iv=0&sign=713230f451cb424c7f2414f2e301d630&time=3";
        String result = null;
        try {
            result = HttpUtil.doGet1(url);
        } catch (IOException e) {
            Constant.IP_ADDRESS = null;
        }

        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject != null) {
            String data = jsonObject.getString("data");
            String ip = JSONObject.parseObject(JSONObject.parseArray(data).getString(0)).getString("ip");
            String port = JSONObject.parseObject(JSONObject.parseArray(data).getString(0)).getString("port");
            Constant.IP_ADDRESS = ip + ":" + port;
            System.out.println(Constant.IP_ADDRESS);
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, Integer.valueOf(port)));
            Constant.client = new OkHttpClient().newBuilder()
                    .followRedirects(false)
                    .followSslRedirects(false)
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .readTimeout(5, TimeUnit.SECONDS)
                    .proxy(proxy)
                    .connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
                    .build();
        } else {
            Constant.IP_ADDRESS = null;
        }


    }
}
